package com.zch.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.base.dialect.PaginationSupport;
import com.base.support.ExcelColumns;
import com.base.support.ExcelFormatter;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;

import com.base.util.DateUtil;
import com.base.util.ExcelExporter;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.zch.dao.model.Activitymessage;
import com.zch.dao.model.CompanyRegisterNew;
import com.zch.dao.model.CompanyRegisterNewExample;
import com.zch.dao.model.Zaiti;
import com.zch.dao.model.ZaitiExample;
import com.zch.dao.model.ZaitiType;
import com.zch.dao.model.ZxMessage;
import com.zch.daoEx.model.ActivitymessageEx;
import com.zch.daoEx.model.ActivitytypeEx;
import com.zch.daoEx.model.ZaitiEx;
import com.zch.service.CompanyRegisterNewService;
import com.zch.service.ZaitiService;

@RequestMapping("zaiti")
@Controller
@LoginFilter
public class ZaitiController extends CoreController{
    
	@Autowired
	private ZaitiService zaitiService;
	@Autowired
    private MessageService messageService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
	    PaginationSupport.byPage(page, rows);
	    Map<String, Object> map = new HashMap<String, Object>();
	    map.put("name", name);
	    List<ZaitiEx> list = zaitiService.selectList(map);
	    
        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(Zaiti zaiti){
		if (StringUtils.isBlank(zaiti.getId())){
			zaitiService.insert(zaiti);
		}else{
			zaitiService.updateByPrimaryKeySelective(zaiti);
			zaiti = zaitiService.selectByPrimaryKey(zaiti.getId());          
            String pushContent = "";
            if (zaiti.getIsAgree() == 2){
                pushContent = "审核未通过！原因:"+zaiti.getNotAgreeReason();
                messageService.findMessage(zaiti.getZaitiName(), pushContent, zaiti.getUserId(), 1);
            }
			
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		Zaiti zaiti = zaitiService.selectByPrimaryKey(id);
		zaiti.setContent(zaiti.getContent().replaceAll("\r\n", "<br/>"));
		return JSONUtils.serialize(zaiti);
	}
	
	@RequestMapping("/findByIds")
    @ResponseBody
    public String finds(String id){
        Zaiti zaiti = zaitiService.selectByPrimaryKey(id);
        zaiti.setContent(zaiti.getContent().replaceAll("\r\n", "<br/>").replaceAll("\r", "<br/>").replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
        return JSONUtils.serialize(zaiti);
    }

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			zaitiService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

    @RequestMapping("/update")
    @ResponseBody
    public String update(Zaiti zaiti){
        int result = zaitiService.updateByPrimaryKeySelective(zaiti);
        if(result == 1){
            zaiti = zaitiService.selectByPrimaryKey(zaiti.getId());    
            String pushContent = "";
            if(zaiti.getIsAgree() == 1){
                 pushContent = "审核通过！";
                 messageService.findMessage(zaiti.getZaitiName(), pushContent, zaiti.getUserId(), 1);
            }
            
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    /*@RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, String id) throws Exception{
        Map<String, Object> map = new HashMap<String ,Object>();
        CompanyRegisterNewExample example = new CompanyRegisterNewExample();
        example.createCriteria();
        List<CompanyRegisterNew> list = companyRegisterNewService.selectByExample(example);
        ExcelExporter exporter = new ExcelExporter();
        
        ExcelColumns[] headers = new ExcelColumns[]{
                new ExcelColumns("companyName","企业名称"),
                new ExcelColumns("zaitiTypeName","载体类型"),
                new ExcelColumns("address","载体地址"),
                new ExcelColumns("area","面积"),
                new ExcelColumns("rental","租金标准"),
                new ExcelColumns("content","载体概况")
        };
        exporter.createWork(list, headers);
        exporter.export(response, null);
    }*/
    
    @RequestMapping("/mylist")
    @ResponseBody
    public String mylist(Integer page, Integer rows, String name){
        PaginationSupport.byPage(page, rows);
        String id = getSessionParameter("id");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("id", id);
        List<ZaitiEx> list = zaitiService.selectMyList(map);
        return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    }
    
    @RequestMapping("/myListSave")
    @ResponseBody
    public String myListSave(Zaiti zaiti){
        zaiti.setUserId(getSessionParameter("id"));
        if(!getSessionParameter("sys").equals("inside")){
            return JSONUtils.serialize(new Ret(-1,"请重新登录..."));
        }
        if (StringUtils.isBlank(zaiti.getId())){
            zaitiService.insert(zaiti);
        }else{
            zaitiService.updateByPrimaryKeySelective(zaiti);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/myZaitilist")
    @ResponseBody
    public String myZaitilist(String name){
        String id = getSessionParameter("id");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("id", id);
        List<ZaitiEx> list = zaitiService.selectAppList(map);
        return JSONUtils.serialize(list);
    }
    
    @RequestMapping("/top")
    @ResponseBody
    public String top(Zaiti zaiti){
        if (StringUtils.isBlank(zaiti.getId())){
            zaiti.setPublishTime(new Date());
            zaitiService.insert(zaiti);
        }else{
            zaiti.setPublishTime(new Date());
            zaitiService.updateByPrimaryKeySelective(zaiti);
        }
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/cancelTops")
    @ResponseBody
    public String cancelTops(Zaiti zaiti){
        if (StringUtils.isBlank(zaiti.getId())){
            zaiti.setPublishTime(null);
            zaitiService.insert(zaiti);
        }else{
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", zaiti.getId());
            int result = zaitiService.cancleTops(map);
        }
        return JSONUtils.serialize(new Ret(0));
    }
}